﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Linq;
using stud.DTOs;
using System.Configuration;
namespace stud.configurations
{
    public class SiteMenuConfig
    {
        public static List<MenuItemInfo> GetMenuItem()
        {
            XElement menuConfig = GetMenuConfig();
            return menuConfig.Element("menu")
                  .Descendants("menuitem")
                  .Select(mn => new MenuItemInfo
                                    {
                                        MenuText = mn.Attribute("text").Value,
                                        NavigateUrl = mn.Attribute("navigateurl").Value,
                                        Roles = mn.Attribute("roles").Value.Split(',').ToList()
                                    }
                  ).ToList();
        }
        private static XElement GetMenuConfig()
        {
            XElement config = (XElement)HttpRuntime.Cache.Get("MenuConfig");
            if (config == null)
            {
                string fileName = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, MenuConfigPath);
                XDocument xDoc = XDocument.Load(fileName); // load the the xml file and get Documnet.
                config = xDoc.Element("SiteMenu");
                HttpRuntime.Cache.Add("MenuConfig", config, new System.Web.Caching.CacheDependency(fileName), DateTime.Now.AddDays(7),
                        TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
            }
            return config;
        }
        private static string MenuConfigPath
        {
            get { return ConfigurationManager.AppSettings["MenuConfigPath"]; }
        }
    }
}
